//
//  ShareViewController.swift
//  cysLibrarySys
//
//  Created by 曹亚索 on 2021/7/5.
//

import UIKit
import ZKProgressHUD

class ShareViewController: UIViewController {

    @IBOutlet weak var containView: UIView!
    @IBOutlet weak var avatarImageView: UIImageView!
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var authorTextField: UITextField!
    @IBOutlet weak var abstractTextField: UITextField!
    @IBOutlet weak var pushInfoButton: UIButton!
    private var picker: UIImagePickerController!
    private let originPoint: CGPoint = .zero
    private let upPoint: CGPoint = CGPoint(x: 0, y: -100)
    
    //MARK:Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        commonInit()
    }
    
    private func commonInit(){
        nameTextField.delegate = self
        authorTextField.delegate = self
        abstractTextField.delegate = self
        setUpAvatarImageView()
    }
    
    private func setUpAvatarImageView(){
        avatarImageView.layer.borderWidth = 2
        avatarImageView.layer.borderColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
        avatarImageView.layer.cornerRadius = avatarImageView.frame.width/10
        avatarImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(showImagePicker(tap:))))
        avatarImageView.isUserInteractionEnabled = true
    }
    
    @objc private func showImagePicker(tap:UIGestureRecognizer){
        switch tap.state {
        case .ended:
            if picker != nil{
                picker.sourceType = .photoLibrary
                self.present(picker, animated: true, completion: nil)
            }else{
                setUpImagePicker()
                picker.sourceType = .photoLibrary
                self.present(picker, animated: true, completion: nil)
            }
        default:
            break
        }
    }
    
    @IBAction func pickImage(_ sender: Any) {
        if picker != nil{
            picker.sourceType = .camera
            present(picker, animated: true, completion: nil)
        }else{
            setUpImagePicker()
            picker.sourceType = .camera
            present(picker, animated: true, completion: nil)
        }
    }
    
    @IBAction func pushInfo(_ sender: Any) {
        if nameTextField.text?.isEmpty == false, authorTextField.text?.isEmpty == false{
            let alert = setUpAlert()
            present(alert, animated: true, completion: nil)
        }else{
            ZKProgressHUD.showInfo("请输入信息")
        }
    }
    
    private func setUpAlert()-> UIAlertController{
        let alert = UIAlertController(title: "是否提交审核？", message: nil, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { [self] (default) in
            let book = Book(face: nil, name: nameTextField.text, author: authorTextField.text, abstract: abstractTextField.text)
            userSelf?.share.append(book)
            LC.pushToCheckPending(bookName: book.name!, bookFace: nil, bookAuthor: book.author!, bookAbstract: book.abstract ?? "")
            ZKProgressHUD.showSuccess("提交成功")
        }))
        alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
        return alert
    }
    
}
//MARK:TextfieldDelegate
extension ShareViewController: UITextFieldDelegate {
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        //view上移
        UIView.animate(withDuration: 0.3) { [self] in
            containView.frame.origin = upPoint
        }
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        //恢复view的frame
        UIView.animate(withDuration: 0.3) { [self] in
            containView.frame.origin = originPoint
        }
        containView.transform = .identity
    }
    
}
//MARK:ImagePicker,Camera
extension ShareViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
        avatarImageView.contentMode = .scaleAspectFill
        avatarImageView.image = image
        picker.dismiss(animated: true, completion: nil)
    }
    
    private func setUpImagePicker(){
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary),UIImagePickerController.isSourceTypeAvailable(.camera){
            picker = UIImagePickerController()
            picker.delegate = self
        }
    }

}
